package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;

import cn.itcast.service.cargo.ContractService;


import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;


@Controller
@RequestMapping("cargo/contract")
public class ContractController extends BaseController {
    @Reference
    private ContractService contractService;

    /**
     * 购销合同：显示购销合同列表
     */

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        //创建条件对象
        ContractExample example = new ContractExample();
        //设置条件通过创建时间倒序排序
        example.setOrderByClause("create_time desc");

        //创建条件表达式
        ContractExample.Criteria criteria = example.createCriteria();
        //设置通过企业id查询
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        //获取用户等级
        Integer degree = getLoginUser().getDegree();

        PageInfo<Contract> pageInfo =null;
        //判断  普通员工
        if(degree == 4){
            criteria.andCreateByEqualTo(getLoginUser().getId());
            pageInfo = contractService.findByPage(example, pageNum, pageSize);
        }
        //部门经理
        else if(degree ==3){
            criteria.andCreateDeptEqualTo(getLoginUser().getDeptId());
            pageInfo = contractService.findByPage(example, pageNum, pageSize);
        }
        //大部门经理
        else if(degree ==2){
            pageInfo = contractService.findContractByDept(getLoginUser().getDeptId(),pageNum,pageSize);
        }

        request.setAttribute("pageInfo", pageInfo);
        return "cargo/contract/contract-list";
    }


    /**
     * 购销合同：进入购销合同添加页面
     */

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }


    /**
     * 购销合同：添加/修改购销合同信息
     */
    @RequestMapping("/edit")
    public String edit(Contract contract) {
        //设置企业信息
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());

        if (StringUtils.isEmpty(contract.getId())) {
            //参数没有购销合同id，执行添加
            //设置创建时间，创建人，创建的部门
            contract.setCreateTime(new Date());
            contract.setCreateBy(getLoginUser().getId());
            contract.setCreateDept(getLoginUser().getDeptId());
            contractService.save(contract);
        } else {
            //有购销合同id,执行更新
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list";
    }


    /**
     * 购销合同：进入购销合同修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model, String id) {
        //通过参数id查询一条购销合同数据
        Contract contract = contractService.findById(id);

        model.addAttribute("contract", contract);
        return "cargo/contract/contract-update";
    }

    /**
     * 购销合同：删除一条购销合同信息
     */

    @RequestMapping("/delete")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list";
    }



    /*
    查看（进入）购销合同页面
     */
    @RequestMapping("/toView")
    public String toView(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-view";
    }

    /*
    *状态：0草稿 1已上报待报运 2 已报运
    * 提交购销合同状态为1已上报待报运
    */
    @RequestMapping("/submit")
    public String submit(String id){
        Contract contract = new Contract();
        //设置修改条件
        contract.setId(id);
        //设置修改的状态值
        contract.setState(1);
        //修改（动态spl,有选择的修改）
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }

    /*
     *状态：0草稿 1已上报待报运 2 已报运
     * 取消购销合同（状态设置为0草稿）
     */
    @RequestMapping("/cancel")
    public String cancel(String id){
        Contract contract = new Contract();
        //设置修改条件
        contract.setId(id);
        //设置修改的状态值
        contract.setState(0);
        //修改（动态spl,有选择的修改）
        contractService.update(contract);
        return "redirect:/cargo/contract/list";
    }



}
